package com.workManager.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.workManager.mapper.TbCostEstimateMapper;
import com.workManager.pojo.TbCostEstimate;
import com.workManager.util.DateUtil;
import com.workManager.util.WebUtil;
import com.workManager.vo.CostEstimateVo;
import com.workManager.vo.CostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

/**
 * Created by stran on 2017/11/1 0001.
 */
@Service
public class CostEstimateServiceImpl implements CostEstimateService{

    @Autowired
    private TbCostEstimateMapper costEstimateMapper;

    public List<CostEstimateVo> query(Map map) {
        return costEstimateMapper.query(map);
    }

    public void add(TbCostEstimate costEstimate) {
        if(null!=costEstimate.getRemark()&&!"".equals(costEstimate.getRemark())){
            costEstimate.setRemark("["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:"+costEstimate.getRemark());
        }
        costEstimateMapper.add(costEstimate);
    }

    public void invalid(TbCostEstimate costEstimate) {
        Map map = new HashMap();
        String remarkArr[] = costEstimate.getRemark().split("\\|");
        String remark = "|["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:"+remarkArr[remarkArr.length-1];
        map.put("remark", remark);
        map.put("id", costEstimate.getId());
        costEstimateMapper.invalid(map);
    }

    @Override
    public PageInfo<CostVo> list(Map<String, Object> map) {
        int pageno = !map.containsKey("pagenum")||map.get("pagenum")==null?1:Integer.parseInt(map.get("pagenum").toString());
        int pagesize = !map.containsKey("pagesize")||map.get("pagesize")==null?10:Integer.parseInt(map.get("pagesize").toString());
        PageHelper.startPage(pageno,pagesize);

        List<CostVo> list = costEstimateMapper.queryCost(map);
        PageInfo<CostVo> pageInfo = new PageInfo<CostVo>(list);
        return  pageInfo;
    }

    @Override
    public Map<String, Object> queryByMonth(Map<String, Object> map) {
        List<Map<String,Object>> list = costEstimateMapper.queryByMonth(map);
        List<BigDecimal> estimateList = new ArrayList<>();
        List<BigDecimal> actualList = new ArrayList<>();
        Set<String> month = new TreeSet<>();
        for(Map<String,Object> m : list){
            month.add((String) m.get("month"));
        }
        for (String m : month){
            for (Map<String,Object> l : list){
                if(m.equals(l.get("month"))){
                    if(l.get("type").equals("1")){
                        estimateList.add((BigDecimal) l.get("amount"));
                        actualList.add(BigDecimal.valueOf(0));
                    }else{
                        actualList.add((BigDecimal) l.get("amount"));
                        estimateList.add(BigDecimal.valueOf(0));
                    }
                }
            }
        }
        Map<String,Object> result = new HashMap<>();
        result.put("month",month);
        result.put("estimateList",estimateList);
        result.put("actualList",actualList);
        return result;
    }

    @Override
    public List<Map<String, Object>> orgList() {
        return costEstimateMapper.orgList();
    }

    @Override
    public void approval(List<Integer> ids) {
        Map map = new HashMap();
        String remark = "|["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:已审批";
        map.put("remark", remark);
        map.put("ids", ids);
        costEstimateMapper.approval(map);
    }

    @Override
    public List<CostEstimateVo> queryGather(Map map) {
        return costEstimateMapper.queryGather(map);
    }

    @Override
    public void approvalByOrg(Integer orgId, Integer projectId) {
        Map map = new HashMap();
        String remark = "|["+ DateUtil.getDate()+":"+ WebUtil.getCurrentUserVO().getUserName()+"]:已审批";
        map.put("remark", remark);
        map.put("orgId", orgId);
        map.put("projectId", projectId);
        costEstimateMapper.approvalByOrg(map);
    }

}
